﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Data;

namespace MoneyWentWhere.DataModel.Binding
{
    public class BindingListEx<T> : BindingList<T>
    {
        public BindingListEx() : base() { }
        public BindingListEx(IList<T> list) : base(list) { }

        public event EventHandler<ListEventArgs> Deleting;

        protected override void RemoveItem(int index)
        {
            if (Deleting != null) 
            { 
                Deleting(this, new ListEventArgs(index)); 
            }

            base.RemoveItem(index); 
        }

        protected override void InsertItem(int index, T item)
        {
            if (base.Contains(item))
            {
                // QQ: throw or just make it a no-op?
                throw new InvalidOperationException("Item already present: " + item);
            }
            base.InsertItem(index, item);
        }
    }

    public class ListEventArgs : EventArgs
    {
        public readonly int Index;
        public ListEventArgs(int index)
        {
            Index = index;
        }
    }
}
